/**
 * Created by sxwu on 14-2-15.
 */


var fields_all = "custlogid,customerid,logauthtype,logcustip,logcustmac,logcurrentflow,logcurrenttime,logupdate,logdowndate";

/**
 * 模块依赖
 */
var db = require("../lib/db");


//查找一个虚拟运营商上已经接入的客户上下线信息
exports.select = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.* FROM customerlog a,customerinfo b WHERE a.customerid=b.customerid and b.userid=?",
        "args": [userid],
        "handler": handler
    });
};

exports.selectMac = function(custmac, handler) {
    db.execQuery({
        "sql": "SELECT a.* FROM customerlog a,customerinfo b WHERE a.logcustmac=b.custmac and b.custmac=?",
        "args": [custmac],
        "handler": handler
    });
};


//根据来源分类查找用户在各整点访问次数
exports.selectForLog = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.* FROM customerlog a,customerinfo b WHERE a.customerid=b.customerid and b.userid=? ",
        "args": [userid],
        "handler": handler
    });
};


//根据来源分类查找用户在各整点访问次数
exports.selectForStat = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT DATE_FORMAT(a.logupdate,'%Y-%m-%d %H:00') hour,COUNT(a.custlogid) count, a.logauthtype authtype FROM customerlog a,customerinfo b " +
            "WHERE a.customerid=b.customerid AND b.userid=? GROUP BY hour,authtype",
        "args": [userid],
        "handler": handler
    });
};

//根据认证类型和时间范围查询
exports.selectForStatByAuthtypeDate = function(range, handler) {
    db.execQuery({
        "sql": "SELECT DATE_FORMAT(a.logupdate,'%Y-%m-%d %H:00') hour,COUNT(a.custlogid) count, a.logauthtype authtype FROM customerlog a,customerinfo b " +
            "WHERE a.customerid=b.customerid AND b.userid=? and a.logauthtype in (?) " +
            "and logupdate between DATE_FORMAT(?,'%Y-%m-%d 00:00') AND  DATE_FORMAT(?,'%Y-%m-%d 23:59') GROUP BY hour,authtype",
        "args": [
            range.userid,
            range.logauthtype,
            range.begindate,
            range.enddate],
        "handler": handler
    });
};
//根据认证类型查询
exports.selectForStatByAuthtype = function(range, handler) {
    db.execQuery({
        "sql": "SELECT DATE_FORMAT(a.logupdate,'%Y-%m-%d %H:00') hour,COUNT(a.custlogid) count, a.logauthtype authtype FROM customerlog a,customerinfo b " +
            "WHERE a.customerid=b.customerid AND b.userid=? and a.logauthtype in (?)  GROUP BY hour,authtype",
        "args": [
            range.userid,
            range.logauthtype],
        "handler": handler
    });
};
//根据时间范围查询
exports.selectForStatByeDate = function(range, handler) {
    db.execQuery({
        "sql": "SELECT DATE_FORMAT(a.logupdate,'%Y-%m-%d %H:00') hour,COUNT(a.custlogid) count, a.logauthtype authtype FROM customerlog a,customerinfo b " +
            "WHERE a.customerid=b.customerid AND b.userid=? " +
            "and logupdate between DATE_FORMAT(?,'%Y-%m-%d 00:00') AND  DATE_FORMAT(?,'%Y-%m-%d 23:59') GROUP BY hour,authtype",
        "args": [
            range.userid,
            range.begindate,
            range.enddate],
        "handler": handler
    });
};


//插入记录
exports.insert = function(customerlog, handler) {
    db.execQuery({
        "sql": "insert into customerlog(customerid,logauthtype,logcustip,logcustmac,logcurrentflow,logcurrenttime,logupdate,logdowndate,sessionid) " +
            "values(?,?,?,?,?,?,?,?,?)",
        "args":[
            customerlog.customerid,
            customerlog.logauthtype,
            customerlog.logcustip,
            customerlog.logcustmac,
            customerlog.logcurrentflow,
            customerlog.logcurrenttime,
            customerlog.logupdate,
            customerlog.logdowndate,
            customerlog.sessionid],
        "handler": handler
    });
};

//删除客户上线日志
exports.updateInactive= function(custlogid, handler) {
    db.execQuery({
        "sql": "update customerlog set custflag=0 WHERE custlogid=?",
        "args": [custlogid],
        "handler": handler
    });

};
//更新记录
exports.update = function(customerlog, handler) {
    db.execQuery({
        "sql": "update customerlog set logcurrentflow=?, logcurrenttime=?, logdowndate=? WHERE customerid=?",
        "args": [
            customerlog.logcurrentflow,
            customerlog.logcurrenttime,
            customerlog.logdowndate,
            customerlog.customerid],
        "handler": handler
    });

};

//更新记录offline
exports.updateOffline = function(customerlog, handler) {
    db.execQuery({
        "sql": "update customerlog set logcurrentflow=?, logcurrenttime=?, logdowndate=? WHERE customerid=?",
        "args": [
            customerlog.logcurrentflow,
            customerlog.logcurrenttime,
            customerlog.logdowndate,
            customerlog.customerid],
        "handler": handler
    });
};
//遍历记录
exports.findAll = function(handler) {
    db.execQuery({
        "sql": "SELECT * FROM customerlog ORDER BY custlogid asc",
        "handler": handler
    });
};
